var convase = document.querySelector('#chess');
var ctx = convase.getContext('2d');


(function (ctx) {//画线
	for(var i=0;i<15;i++)
	{
		var x = (i+0.5)*30,y1=15,y2=435;
		ctx.strokeStyle = '#aaa';
		ctx.moveTo(x,y1)
		ctx.lineTo(x,y2)
		ctx.stroke()
		ctx.moveTo(y1,x)
		ctx.lineTo(y2,x)
		ctx.stroke()
	}
	
})(ctx);

(function (ctx,w) {
	w.G = {
		over:false
	};
	w.G.luoZi=function (a,b,black) {
		var x = Math.floor(a*30+15),
			y = Math.floor(b*30+15);
		ctx.beginPath()
		ctx.arc(x,y,13,0,Math.PI*2)
		ctx.closePath()
		if(black){
			var rgl=ctx.createRadialGradient(x,y,13,x,y,0);
			rgl.addColorStop(0,'#000');
			rgl.addColorStop(1,'#fff');
		}else{
			var rgl=ctx.createRadialGradient(x,y,13,x,y,0);
			rgl.addColorStop(0,'#999');
			rgl.addColorStop(1,'#fff');
		}
		
		ctx.fillStyle = rgl;
		ctx.fill()
	}
})(ctx,window);


(function (w) {
	w.convase.onclick= function (e) {
		if(G.over)return;

		var x = Math.floor(e.offsetX/30);
		var y = Math.floor(e.offsetY/30);
		if(!zis[x+":"+y].empty)return;
		G.luoZitor.do(x,y);
	}
	w.G.emit=function (name) {
		var event =new Event(name);
		event.args = Array.prototype.slice.call(arguments,1);
		window.dispatchEvent(event);
	}
	w.G.on = function (name,cb) {
		window.addEventListener(name,function (e) {
			cb.apply(window,e.args);
		})
	}
	w.G.luoZitor = {
		black : true
	};
	w.G.luoZitor.do=function (x,y) {
		w.G.luoZi(x,y,this.black=!this.black);
		if(!this.black){
			w.G.emit('white-zi-down',x,y);//白子落下
		}else{
			w.G.emit('black-zi-down',x,y);//黑子落下
		}
	}
})(window);

(function (w) {
	var wins = [];
	var zis = {
		length:0
		
	};
	for(var i =0;i<15;i++){
		for(var j =0;j<15;j++){
			zis[i+":"+j]={wins:[],empty:true};
			zis.length++;
		}
	}
	function getObj() {
		return {
				over:0,
				white:true,
				black:true,
				all:true,
				poins:[]
			};
	}
	for(var i =0;i<15;i++){//y 行
		for(var j =0;j<11;j++){//x 列
			var obj = getObj();
			for(var k =0;k<5;k++){
				obj.poins.push((j+k)+":"+i);
				zis[(j+k)+":"+i].wins.push(obj);
			}
			wins.push(obj);
			
		}
	}
	for(var i =0;i<15;i++){//x 列
		for(var j =0;j<11;j++){//y 行
			var obj = getObj();
			for(var k =0;k<5;k++){
				obj.poins.push(i+":"+(j+k));
				zis[i+":"+(j+k)].wins.push(obj);
			}
			wins.push(obj);
			
		}
	}
	for(var i =0;i<11;i++){//x 列
		for(var j =0;j<11;j++){//y 行
			var obj = getObj();
			for(var k =0;k<5;k++){
				obj.poins.push((i+k)+":"+(j+k));
				zis[(i+k)+":"+(j+k)].wins.push(obj);
			}
			wins.push(obj);
			
		}
	}
	for(var i =0;i<11;i++){//y 行
		for(var j =14;j>3;j--){//x 列
			var obj = getObj();
			for(var k =0;k<5;k++){
				obj.poins.push((j-k)+":"+(i+k));
				zis[(j-k)+":"+(i+k)].wins.push(obj);
			}
			wins.push(obj);
		}
	}

	w.wins =wins;
	w.zis =zis;

	
})(window);



G.on('white-zi-down',function (x,y) {
	//这个位置上的所有方案评估
	var over = false;
	if(!zis[x+":"+y].empty)return;
	zis[x+":"+y].empty=false;
	zis[x+":"+y].wins
	.forEach(function (item) {
		item.black = false;//该位置上所有黑方的方案无效了
		item.white || (item.all=false);//如果白方也无效了，则该方案无效
		item.white && item.over ++ ;
		if(item.over==5){
			G.emit('is-over','white');
			over = true;
			return;
		}
		item.over > G.maxOver && (G.maxOver = item.over)
	});
	over || G.emit('waite-black');
})

G.on('black-zi-down',function (x,y) {
	//这个位置上的所有方案评估
	var over = false;
	if(!zis[x+":"+y].empty)return;
	zis[x+":"+y].empty=false;
	zis[x+":"+y].wins
	.forEach(function (item) {
		item.white = false;//该位置上所有黑方的方案无效了
		item.black || (item.all=false);//如果白方也无效了，则该方案无效
		item.black && item.over ++ ;
		if(item.over==5){
			G.emit('is-over','black');
			over = true;
			return;
		}
		item.over > G.maxOver && (G.maxOver = item.over)
	});
	over || G.emit('waite-white');
})

G.on('is-over',function (a) {
	G.over=true;
	alert("游戏结束"+(a=="black"?"黑方":"白方")+"获胜");
	document.getElementById('alert').innerHTML="游戏结束"+(a=="black"?"黑方":"白方")+"获胜";
});

G.on('waite-white',function () {
	document.getElementById('alert').innerHTML="等待白棋落子";
});
G.on('waite-black',function () {
	document.getElementById('alert').innerHTML="等待黑棋落子";
	aiGo('black');
});


function luoziName(zi) {//根据名称落子
	zi = zi.split(":");
	G.luoZitor.do(zi[0],zi[1]);
}

function aiGo(role) {
	//找出所有的有效方案
	var ww = wins.filter(item=>item.all);
	ww = ww.sort((a,b)=>b.over-a.over);
	// zz = ww.filter(item=>item.over>=2);
	// if(zz.length==0)zz = ww;

	//四子连珠必连
	for(var i in ww){
		if(ww[i].over==4){
			// debugger;
			var zi = ww[i].poins.filter(item=>zis[item].empty);
			luoziName(zi[0]) ;
			return;
		}else if(ww[i]==3){
			
		}
	}
	//三资三子无障碍必连
	//二子交叉必连
	var x = 0,
		zz=[];
	for(var i in ww){
		if(ww[i].over<x){
			break;
		}else{
			zz.push(ww[i]);
			x=ww[i].over;
		}
	}
	var you = selectZi(zz);
	you = you.split(":");
	G.luoZitor.do(you[0],you[1]);
	// console.log(you);
}

function selectZi(zz) {
	var zizrr={};//可匹配有效方案最多的子
	var llen=0;
	zz.forEach(item=>{
		item.poins.forEach(tt=>{
			if(zizrr[tt]===undefined)zizrr[tt]=1;
			else zizrr[tt]++;
		});
	});
	for(var i in zizrr){//去掉已经落子的子
		if(!zis[i].empty)
			delete zizrr[i];
		else{
			llen++;
		}
	}

	if(llen==0){
		G.emit('he-qi');
	}
	var you ,max = 0;
	for(var i in zizrr){
		if(zizrr[i]>max){
			max = zizrr[i];
			you=i;
		}else if(zizrr[i]==max){
			if(zis[i].wins.length>zis[you].wins.length){
				you=i;
			}
		}
		
	}
	return you;
}
